package com.ruiysoft.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruiysoft.controller.sys.api.SysUserApi;
import com.ruiysoft.controller.sys.api.service.SysUserApiService;
import com.ruiysoft.controller.sys.res.EntryUserResponse;
import com.ruiysoft.controller.sys.res.UserShopResponse;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.entity.sys.SysUser;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.pub.entity.PageInfo;
import com.ruiysoft.security.entity.EntryUser;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 化增光 on 2019/1/4.
 */
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2018-11-02T18:10:17.406+08:00")

@RestController
public class SysUserApiController implements SysUserApi{

    private static final Logger log = LoggerFactory.getLogger(SysUserApiController.class);

    private final ObjectMapper objectMapper;

    @Resource
    SysUserApiService sysUserApiService;

    @org.springframework.beans.factory.annotation.Autowired
    public SysUserApiController(ObjectMapper objectMapper, HttpServletRequest request) {
        this.objectMapper = objectMapper;
    }

    @Override
    public Result<List<UserShopResponse>> getUserShops() {
        SysUser sysUser = LoginContextUtil.instance().getSysUser();
        List<UserShopResponse> responses = sysUserApiService.getUserShops(sysUser.getId());
        return ResultGenerator.genSuccessResult(responses);

    }

    @Override
    public Result<EntryUserResponse> getLoginOnUser() {

        try {
            EntryUser entryUser = LoginContextUtil.instance().getEntryUser();
//            SysMicroShop microShop = LoginContextUtil.instance().getEntryMicroShop();
//            SysUser user = entryUser.getSysUser();
            EntryUserResponse response = new EntryUserResponse()
                    .setMobile(entryUser.getSysUser().getMobile())
                    .setRealName(entryUser.getSysUser().getRealName());
            if (null != entryUser.getPersonnel()){
                response.setNickName(entryUser.getPersonnel().getNickname());
                response.setHeadImgUrl(entryUser.getPersonnel().getHeadImgUrl());
            }
            return ResultGenerator.genSuccessResult(response);
        } catch (Exception e) {
            log.error("Couldn't serialize response for content type application/json", e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<PageInfo<UserShopResponse>> getInnerUserShops(@NotNull @ApiParam(value = "分页开始页", required = true) @Valid @RequestParam(value = "pageStart", required = true) Integer pageStart, @NotNull @ApiParam(value = "每页显示多少条数据", required = true) @Valid @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            IPage<SysMicroShop> responses = sysUserApiService.getAllShops(pageStart,pageSize);

            List<UserShopResponse> list = responses.getRecords().stream()
                    .filter(e->StringUtils.isNotBlank(e.getShopName()))
                    .map(r ->{
                        UserShopResponse response = sysUserApiService.selectUserShopInfoByShopId(r.getId());
                        response.setId(r.getId());
                        response.setShopName(r.getShopName());
                        response.setShopImg(r.getShopImg());
                        response.setCreateTime(r.getCreateTime());
                        response.setExpiryTime(r.getCreateTime());
                        response.setMobile(r.getTelephone());
                return response;
            }).collect(Collectors.toList());
            return ResultGenerator.genSuccessResult(new PageInfo<UserShopResponse>(list, (Page) responses));
        }catch (Exception e){
            log.error("Couldn't serialize response for content type application/json", e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

//    @Override
//    public Result<Void> chooseUserShop(@ApiParam(value = "店铺主键") @RequestParam(value = "shopId", required = true) @Valid Long shopId) {
//        String accept = request.getHeader("Accept");
//        if (accept != null && accept.contains("application/json")) {
//            try {
//                SysUser sysUser = LoginContextUtil.instance().getEntryUser().getSysUser();
//                sysUserApiService.chooseUserShop(sysUser.getId(),sysUser.getPersonnelId(),shopId);
//                return ResultGenerator.genSuccessResult();
//            } catch (Exception e) {
//                log.error("Couldn't serialize response for content type application/json", e);
//                return ResultGenerator.genFailResult(e.getMessage());
//            }
//        }
//        return ResultGenerator.genAcceptResult("accept is :" +accept);
//    }
//
//    @Override
//    public Result<ShopResponse> getUserLoginShop() {
//        String accept = request.getHeader("Accept");
//        if (accept != null && accept.contains("application/json")) {
//            try {
//                SysMicroShop microShop = LoginContextUtil.instance().getMicroShopLoginByUserId();
//                ShopResponse response = sysUserApiService.getUserLoginShop(microShop.getId());
//                return ResultGenerator.genSuccessResult(response);
//            } catch (Exception e) {
//                log.error("Couldn't serialize response for content type application/json", e);
//                return ResultGenerator.genFailResult(e.getMessage());
//            }
//        }
//        return ResultGenerator.genAcceptResult("accept is :" +accept);
//    }

    @Override
    public Result<SysUser> getShopkeeper(Long shopId) {

            try {
                SysUser user = sysUserApiService.getShopkeeper(shopId);
                return ResultGenerator.genSuccessResult(user);
            } catch (Exception e) {
                log.error("Couldn't serialize response for content type application/json", e);
                return ResultGenerator.genFailResult(e.getMessage());
            }
    }

    @Override
    public Result<Void> deleteSysUserById(@PathVariable("id") Long id) {
        sysUserApiService.deleteSysUserById(id);
        return ResultGenerator.genSuccessResult();
    }
}
